<?php
namespace system\model;


use houdunwang\model\Model;

class Admin extends Model
{
	//private $tabe='weixincms';
	protected $table = "weixincms";

	public function login()
	{
		//return p($_POST);

			$res = Validate::make([
				['admin_username', 'isnull', '用户名不能为空', self::MUST_VALIDATE],
				['admin_password', 'isnull', '密码不能为空', self::MUST_VALIDATE],
				['code', 'isnull', '验证码不能为空', self::MUST_VALIDATE]
			]);
			if ($res === false) {
				return ['valid' => 0, 'msg' => Validate::getError()];

			}
			$userInfo = $this->where('admin_username', $_POST['admin_username'])->where('admin_password', Crypt::encrypt($_POST['admin_password']))->first();

			//return p($userInfo);
			if (!$userInfo) {
				//用户名密码不对
				return ['valid' => 0, 'msg' => '用户名或者密码不正确'];
			}

			if (strtolower(Code::get()) != strtolower($_POST['code'])) {
				return ['valid' => 0, 'msg' => '验证码不正确'];
			}

		Session::set('admin.admin_id',$userInfo['admin_id']);
		Session::set('admin.admin_username',$userInfo['admin_username']);
		return ['valid'=>1,'msg'=>'登录成功'];
	}

}

